<!doctype html>
<html lang="en">
<head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet"
          integrity="sha384-giJF6kkoqNQ00vy+HMDP7azOuL0xtbfIcaT9wjKHr8RbDVddVHyTfAAsrekwKmP1" crossorigin="anonymous">

    <!-- Option 1: Bootstrap Bundle with Popper -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/js/bootstrap.bundle.min.js"
            integrity="sha384-ygbV9kiqUc6oa4msXn9868pTtWMgiQaeYH7/t7LECLbyPA2x65Kgf80OJFdroafW"
            crossorigin="anonymous"></script>

    <link href="css/main.css" rel="stylesheet">

    <script src="js/main.js"></script>
    <script src="js/echarts.min.js"></script>

    <title>奶茶店后台</title>
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-light bg-light shadow-sm">
    <div class="container-fluid">
        <a class="navbar-brand" href="/main.html">奶茶店后台</a>
        <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav"
                aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
            <span class="navbar-toggler-icon"></span>
        </button>
        <div class="collapse navbar-collapse" id="navbarNav">
            <ul class="navbar-nav">
                <li class="nav-item">
                    <a class="nav-link active" aria-current="page" href="/order.html">订单管理</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="/users.html">用户管理</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="/product.html">商品管理</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="/productSort.html">商品分类管理</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="/activity.html">活动管理</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="/userInfo.html">个人中心</a>
                </li>
            </ul>
        </div>
    </div>
</nav>
<div class="d-flex flex-column justify-content-center align-items-center">

    <div class="alert alert-primary position-absolute w-25 text-center" role="alert" id="alert" style="top: 100px; z-index: 1055;" hidden>
    </div>
    <script src="js/general.js"></script>


    <div class="title-holder w-75 mt-lg-3 mb-lg-4 p-lg-5">
        <h1>统计数据</h1>
    </div>

    <button type="button" class="btn btn-primary mb-lg-4" onclick="exportExcel()">导出订单表格</button>

    <div id="main" class="w-75 h-50" style="width: 800px; height: 800px;"></div>

    <script type="text/javascript">
        // 基于准备好的dom，初始化echarts实例
        fetch("https://ali.ikenway.net:18443/api/product/findAll", {
            method: 'GET',
            headers: {
                'Content-Type': 'application/json',
                'Authorization': token
            }
        }).then(res => {
            res.json().then(json => {
                alertMsg(json.msg);
                if (json.code == 200) {
                    let products = json.data;
                    let productNames = [];
                    let sellCounters = [];

                    for (let i = 0; i < products.length && i < 10; i++) {
                        productNames.push(products[i].productName);
                        sellCounters.push(products[i].sellCounter);
                    }

                    // 指定图表的配置项和数据
                    let option = {
                        title: {
                            text: '产品销量统计'
                        },
                        tooltip: {},
                        legend: {
                            data: ['销量']
                        },
                        xAxis: {
                            data: productNames
                        },
                        yAxis: {},
                        series: [{
                            name: '销量',
                            type: 'bar',
                            data: sellCounters
                        }]
                    };

                    let myChart = echarts.init(document.getElementById('main'));
                    myChart.setOption(option);
                } else {
                    window.location.href = "/login.html"
                }
            })
        })

    </script>

</div>

</body>
</html>
